<?php

namespace client;

use bank\Bank;
use account\Deposit;
use account\Credit;
use Exception;

class Client {
	function __autoload($class_name) {
		include $class_name . '.php';
	}
	private $name;
	private $address;
	private $cash;
	private $salary;
	private $deposits = array ();
	private $credits = array ();
	public function __construct($name, $address, $cash, $salary) {
		$this->setName ( $name );
		$this->setAddress ( $address );
		$this->setCash ( $cash );
		$this->setSalary ( $salary );
	}
	// Setters
	public function setName($name) {
		if (! is_string ( $name )) {
			throw new Exception ( "<p>$name is an invalid value for name</p>" );
		}
		$this->name = $name;
	}
	public function setAddress($address) {
		if (! is_string ( $address )) {
			throw new Exception ( "<p>$address is an invalid value for address</p>" );
		}
		$this->address = $address;
	}
	public function setCash($cash) {
		if (is_string ( $cash )) {
			throw new Exception ( 'Client cash can not be string.' );
		}
		if (! is_numeric ( $cash )) {
			throw new Exception ( 'Client cash must be numeric' );
		}
		if ($cash < 0) {
			throw new Exception ( 'Client cash can not be negative number' );
		}
		$this->cash = $cash;
	}
	public function setSalary($salary) {
		if (! is_int ( $salary ) && ! is_double ( $salary )) {
			throw new Exception ( "<p>$salary is an invalid value for monthly salary." );
		}
		if ($salary < 0) {
			throw new Exception ( "<p>Salary cannot be a negative number.</p>" );
		}
		$this->salary = $salary;
	}
	// Getters
	public function getName() {
		return $this->name;
	}
	public function getAddress() {
		return $this->address;
	}
	public function getCash() {
		return $this->cash;
	}
	public function getSalary() {
		return $this->salary;
	}
	public function getCredits() {
		return $this->credits;
	}
	public function getDeposits() {
		return $this->deposits;
	}
	// Actions
	public function askForCredit(Bank $bank, Credit $credit) {
		if ($bank->giveCredit ( $this, $credit ) == true) {
			$this->credits [] = $credit;
			$this->setCash ( $this->cash += $credit->getBalance () );
		} 
	}
	public function makeCreditPayment(Bank $bank, Credit $credit) {
		if ($this->cash > $credit->getSum ()) {
			$bank->paidCredit ( $this, $credit );
			$this->setCash ( $this->cash -= $credit->getSum () );
		} else {
			echo "<p>$name does not have enough cash to make a credit payment right now.</p>";
		}
	}
	public function makeDeposit(Bank $bank, Deposit $deposit) {
		if ($this->cash < $deposit->getBalance ()) {
			echo "<p>$this->name does not have sufficient funds to make such deposit.";
		} else {
			// echo 'abadee';
			$bank->takeDeposit ( $deposit );
			$this->deposits [] = $deposit;
			$this->setCash ( $this->cash -= $deposit->getBalance () );
		}
	}
	public function __toString() {
		$result = '
		<p>Client name: '. $this->getName() .'</p>
		<p>Client adress: '. $this->getAddress() .'</p>
		<p>Client cash: '. $this->getCash() .'</p>
		<p>Client salary: '. $this->getSalary() .'</p>
		<p>Deposits:';
		foreach ($this->getDeposits() as $deposit) {
			$result .= '<p>Deposit type: '. get_class($deposit) .'</p>
			<p>Deposit name: '. $deposit->getName() .'</p>
			<p>Deposit percent: '. $deposit->getPercent() .'</p>
			<p>Deposit period: '. $deposit->getPeriod() .'</p>
			<p>Deposit balance: '. $deposit->getBalance() .'</p>
			<p>Deposit sum: '. $deposit->getSum() .'</p><br/>';
		}
		$result .= '<p>Credits:';
		foreach ($this->getCredits() as $credit) {
			$result .=
			'<p>Credit type: '. get_class($credit) .'</p>
			<p>Credit name: '. $credit->getName() .'</p>
			<p>Credit percent: '. $credit->getPercent() .'</p>
			<p>Credit period: '. $credit->getPeriod() .'</p>
			<p>Credit balance: '. $credit->getBalance() .'</p>
			<p>Credit sum: '. $credit->getSum() .'</p><br/>';
		}
		$result .= '</p><br/>';
		return $result;
	}
}

?>